package thread;

//死锁情况二：两个线程，两把锁，在获取到一把锁后，尝试获取对方的锁
//解决死锁：1.尽量避免嵌套锁（有时候不可避免） 2.规定锁顺序，永远获取序号小的锁
public class Demo14 {
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(()->{
            synchronized (locker1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (locker2){
                    System.out.println("t1两个线程都获取到");
                }
            }
        });

        Thread t2 = new Thread(()->{
            synchronized (locker2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (locker1){
                    System.out.println("t1两个线程都获取到");
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }



}
